package org.dromara.product.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.product.domain.bo.KyStockInRelationsBo;
import org.dromara.product.domain.vo.KyStockInRelationsVo;
import org.dromara.product.domain.KyStockInRelations;
import org.dromara.product.mapper.KyStockInRelationsMapper;
import org.dromara.product.service.IKyStockInRelationsService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 入库关联Service业务层处理
 *
 * @author kuyu
 * @date 2023-12-29
 */
@RequiredArgsConstructor
@Service
public class KyStockInRelationsServiceImpl implements IKyStockInRelationsService {

    private final KyStockInRelationsMapper baseMapper;

    /**
     * 查询入库关联
     */
    @Override
    public KyStockInRelationsVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询入库关联列表
     */
    @Override
    public TableDataInfo<KyStockInRelationsVo> queryPageList(KyStockInRelationsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<KyStockInRelations> lqw = buildQueryWrapper(bo);
        Page<KyStockInRelationsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询入库关联列表
     */
    @Override
    public List<KyStockInRelationsVo> queryList(KyStockInRelationsBo bo) {
        LambdaQueryWrapper<KyStockInRelations> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<KyStockInRelations> buildQueryWrapper(KyStockInRelationsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<KyStockInRelations> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getProductId() != null, KyStockInRelations::getProductId, bo.getProductId());
        lqw.eq(bo.getWarehouseId() != null, KyStockInRelations::getWarehouseId, bo.getWarehouseId());
        lqw.eq(bo.getBeforeNum() != null, KyStockInRelations::getBeforeNum, bo.getBeforeNum());
        lqw.eq(StringUtils.isNotBlank(bo.getBathInNo()), KyStockInRelations::getBathInNo, bo.getBathInNo());
        lqw.eq(bo.getInRecordId() != null, KyStockInRelations::getInRecordId, bo.getInRecordId());
        lqw.eq(StringUtils.isNotBlank(bo.getInRecordNo()), KyStockInRelations::getInRecordNo, bo.getInRecordNo());
        lqw.eq(bo.getStatus() != null, KyStockInRelations::getStatus, bo.getStatus());
        lqw.eq(bo.getApproveLevel() != null, KyStockInRelations::getApproveLevel, bo.getApproveLevel());
        lqw.eq(bo.getPrintNum() != null, KyStockInRelations::getPrintNum, bo.getPrintNum());
        lqw.eq(bo.getMid() != null, KyStockInRelations::getMid, bo.getMid());
        lqw.eq(bo.getSupplyId() != null, KyStockInRelations::getSupplyId, bo.getSupplyId());
        lqw.eq(StringUtils.isNotBlank(bo.getImages()), KyStockInRelations::getImages, bo.getImages());
        lqw.eq(bo.getTransOutId() != null, KyStockInRelations::getTransOutId, bo.getTransOutId());
        lqw.eq(bo.getNumber() != null, KyStockInRelations::getNumber, bo.getNumber());
        lqw.eq(bo.getPrice() != null, KyStockInRelations::getPrice, bo.getPrice());
        lqw.eq(bo.getTotalMoney() != null, KyStockInRelations::getTotalMoney, bo.getTotalMoney());
        lqw.eq(StringUtils.isNotBlank(bo.getItem()), KyStockInRelations::getItem, bo.getItem());
        lqw.eq(StringUtils.isNotBlank(bo.getProductInfo()), KyStockInRelations::getProductInfo, bo.getProductInfo());
        lqw.eq(bo.getZoneId() != null, KyStockInRelations::getZoneId, bo.getZoneId());
        lqw.eq(StringUtils.isNotBlank(bo.getMark()), KyStockInRelations::getMark, bo.getMark());
        lqw.eq(bo.getInTypeId() != null, KyStockInRelations::getInTypeId, bo.getInTypeId());
        lqw.eq(StringUtils.isNotBlank(bo.getCustom()), KyStockInRelations::getCustom, bo.getCustom());
        lqw.eq(bo.getFromWarehouseId() != null, KyStockInRelations::getFromWarehouseId, bo.getFromWarehouseId());
        lqw.eq(bo.getFromZoneId() != null, KyStockInRelations::getFromZoneId, bo.getFromZoneId());
        return lqw;
    }

    /**
     * 新增入库关联
     */
    @Override
    public Boolean insertByBo(KyStockInRelationsBo bo) {
        KyStockInRelations add = MapstructUtils.convert(bo, KyStockInRelations.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改入库关联
     */
    @Override
    public Boolean updateByBo(KyStockInRelationsBo bo) {
        KyStockInRelations update = MapstructUtils.convert(bo, KyStockInRelations.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(KyStockInRelations entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除入库关联
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
